<?php

declare(strict_types=1);

namespace App\Controller;

use App\Constants\ErrorCode;
use App\Service\AdvanceWithdrawService;
use Psr\Http\Message\ResponseInterface;
use Hyperf\Di\Annotation\Inject;

/**
 * 提现
 * Class WithdrawController
 * @package App\Controller
 */
class WithdrawController extends AbstractController
{
    /**
     * @Inject
     * @var AdvanceWithdrawService
     */
    private $withDraw;

    /**
     * 申请提现
     * @return ResponseInterface
     */
    public function apply(): ResponseInterface
    {
        $user_id = (int)$this->getAdminId();
        // 1用户账户提现 2商家销售额提现 3商家保证金提现
        $type = (int)$this->request->input('type', 1);
        $draw_type = $this->request->input('draw_type', '');
        $amount = (float)$this->request->input('amount', 0.00);
        if (!in_array($draw_type, ['alipay', 'wxpay'])) {
            return $this->jsonResponse->error(ErrorCode::NORMAL_ERROR, '提现方式错误');
        }
        $re = $this->withDraw->apply($user_id, $type, $amount, $draw_type);
        if (!$re) {
            return $this->jsonResponse->error(ErrorCode::NORMAL_ERROR, '操作失败，请稍后重试');
        }
        return $this->jsonResponse->success([], 'success!');
    }

    /**
     * 获取提现申请列表
     * @return ResponseInterface
     */
    public function getApplyList(): ResponseInterface
    {
        $user_id = (int)$this->getAdminId();
        if (empty($user_id)) {
            return $this->jsonResponse->success([], '未登录请先登录');
        }
        // 1用户账户提现 2商家销售额提现 3商家保证金提现
        $type = (int)$this->request->input('type', 1);
        $data = $this->withDraw->getApplyList($type);
        $return = $this->paginate($data)->toArray();
        $return['count'] = $this->withDraw->statStateCount($type);
        return $this->jsonResponse->success($return, 'success');
    }

}